package com.reformer.commons.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.reformer.commons.constant.ErrorMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.util.Date;

public class ResponseInfo<T> implements java.io.Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseInfo.class);
    private static SerializeConfig mapping = new SerializeConfig();

    static {
        mapping.put(Date.class, new SimpleDateFormatSerializer("yyyyMMddHHmmss"));
        mapping.put(java.sql.Date.class, new SimpleDateFormatSerializer("yyyyMMddHHmmss"));
        mapping.put(Timestamp.class, new SimpleDateFormatSerializer("yyyyMMddHHmmss"));
    }

    private String id;
    private String cmd;
    private String ver;
    private String errno = "500";
    private String errtext;
    private String uuid;
    private T data;

    public ResponseInfo() {
        super();
    }

    public ResponseInfo(String id, String cmd, String errno, String errtext, T data) {
        super();
        this.id = id;
        this.cmd = cmd;
        this.errno = errno;
        this.errtext = errtext;
        this.data = data;
    }

    public ResponseInfo(String errno, String errtext) {
        super();
        this.errno = errno;
        this.errtext = errtext;
    }

    public ResponseInfo(ErrorMsg errorMsg) {
        this(errorMsg.getErrno(), errorMsg.getErrmsg());
    }

    public ResponseInfo(ErrorMsg errorMsg, T data) {
        this(errorMsg);
        this.data = data;
    }

    public void setErrorMsg(ErrorMsg errorMsg) {
        this.errno = errorMsg.getErrno();
        this.errtext = errorMsg.getErrmsg();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getErrno() {
        return errno;
    }

    public void setErrno(String errno) {
        this.errno = errno;
    }

    public String getErrtext() {
        return errtext;
    }

    public void setErrtext(String errtext) {
        this.errtext = errtext;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getVer() {
        return ver;
    }

    public void setVer(String ver) {
        this.ver = ver;
    }

    @Override
    public String toString() {
        LOGGER.info(JSON.toJSONString(this, mapping));
        return JSON.toJSONString(this, mapping);
    }
}
